import * as WeChat from 'react-native-wechat';
import { ToastToUser } from './CustomToast';
import Alipay from 'react-native-s-alipay';
import { Platform } from 'react-native';
const isIos = Platform.OS == 'ios';
//支付宝支付
export async function alipay(orderInfo) {
    let response = await Alipay.pay(orderInfo);
    console.log('response = ',response);
    var result = '';
    if (gDevice.ios) {
        result = response[0];
    } else {
        result = response;
    }
    if (result.resultStatus == '9000' || result.code == '9000') {
        return new Promise((resolve,reject)=>{
            resolve(0)
        }
        )
    } else {
        return new Promise((resolve,reject)=>{
            resolve(-1)
        }
        )
    }
}
//微信初始化
export function registerApp() {
    try {
        WeChat.registerApp('wxab67c33e32782e01');
    } catch (e) {
    }
}
//微信登录
export function WeChatLogin() {
    return new Promise((resolve, reject) => {
        WeChat.isWXAppInstalled()
            .then((isInstalled) => {
                if (isInstalled) {
                    WeChat.sendAuthRequest('snsapi_userinfo')
                        .then(responseCode => {
                            resolve(responseCode.code)
                        })
                        .catch(err => {
                            ToastToUser('授权错误:' + err.message)
                            resolve('-1')
                        })
                } else {
                    resolve('-2')
                    ToastToUser('微信未安装')
                }
            })
    })

}
//微信支付
export function WeChatPay(data) {
    console.log('data = ',data)
    WeChat.isWXAppInstalled()
        .then((isInstalled) => {
            console.log('isInstalled')
            if (isInstalled) {
                let param = {
                    partnerId: data.partnerid,  // 商家向财付通申请的商家id
                    prepayId: data.prepayId,   // 预支付订单
                    nonceStr: data.nonceStr,   // 随机串，防重发
                    timeStamp: data.timestamp,  // 时间戳，防重发.
                    package: data.packageStr,    // 商家根据财付通文档填写的数据和签名
                    sign: data.sign       // 商家根据微信开放平台文档对数据做的签名
                }
                console.log('param = ',param)
                WeChat.pay(param)
                    .then(requestJson => {
                        // 支付成功回调
                        if (requestJson.errCode === 0) {
                            // props.navigation.navigate('annualFeePayDone');
                        } else {
                            // props.navigation.navigate('annualFeePayFailed');
                        }
                    })
                    .catch(err => {
                        if (errCode === -2) {
                            // 用户取消，无需处理。发生场景：用户不支付了，点击取消，返回APP。
                            return;
                        }
                    });
            } else {
                ToastToUser('微信未安装')
            }
        })
        .catch(err => {
            console.log('err = ',err)
            ToastToUser('微信支付失败')
        })
}

/**
 * 分享网页链接到微信好友
 * @param {} data 
 */
export function shareWebToSession(data){

    WeChat.shareToSession({
        title: data.title,
        description: data.description,
        type: 'news',       // 支持text/image/news/file等
        webpageUrl: data.webpageUrl, 
        thumbImage: data.thumbImage 
      }).then((result) => {

      }).catch((error) => {

      });
}

export function shareWebToTimeline(data){

    WeChat.shareToTimeline({
        title: data.title,
        description: data.description,
        type: 'news',    
        webpageUrl: data.webpageUrl, 
        thumbImage: data.thumbImage 
      }).then((result) => {

      }).catch((error) => {

      });
}



